/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.umlutils;

import java.util.Iterator;

/**
 * 
 * 
 * @author NG206084
 * 
 * This class encapsulates an <code>org.eclipse.uml2.uml.Model</code>.
 * It's the entry point to browse the model
 */
public class Model extends Package {
    
	/**
	 * 
	 */
	private org.eclipse.uml2.uml.Model uml2Model;

    /**
     * 
     * 
     * @param uml2Model 
     */
    public Model(org.eclipse.uml2.uml.Model uml2Model){
        super(uml2Model);
        this.uml2Model = uml2Model;    
  
    }
    
    /**
     * 
     * 
     * @return Returns the uml2Model.
     */
    public org.eclipse.uml2.uml.Model getUml2Model() {
        return uml2Model;
    }
	 
 	/**
 	 * Retrieve a type owned by a Pacakage Import.
 	 * 
 	 * @param typeName the wishing type
 	 * @param model the model owning the package import
 	 * @param packageImportName the package import name handling the type
 	 * 
 	 * @return the type, or null if not found
 	 */
	public org.eclipse.uml2.uml.Type getTypeFromPackageImport(String packageImportName, String typeName){
		org.eclipse.uml2.uml.Type type=null;
		
		org.eclipse.emf.common.util.EList list= getUml2Model().getPackageImports();
		java.util.Iterator i = list.iterator();
		org.eclipse.uml2.uml.PackageImport currentPackage;
		while ((i.hasNext()) && (type==null)){
			currentPackage = (org.eclipse.uml2.uml.PackageImport) i.next();
			if (currentPackage.getImportedPackage().getName().equals(packageImportName)){
				type=currentPackage.getImportedPackage().getOwnedType(typeName);
			}
		}
		if (type==null){
			// Warning type not found in package (packageImportName)
		}
		return type;
	}
	 
 	/**
 	 * Retrieve the first package met with the specified stereotype.
 	 * 
 	 * @param stereotypeName 
 	 * @param stereotypeName, name of the stereotype applied to the package
 	 * 
 	 * @return the package, or null if not found
 	 */
	public org.eclipse.uml2.uml.Package getPackageWithThisStereotype(String stereotypeName){

		org.eclipse.emf.common.util.EList list= getUml2Model().getOwnedMembers();
		java.util.Iterator i = list.iterator();
		com.cea.papyrus.umlutils.Package currentPackage;
		while (i.hasNext()){
			currentPackage = new com.cea.papyrus.umlutils.Package((org.eclipse.uml2.uml.Package) i.next());			
			if (currentPackage.hasStereotype(stereotypeName)){
				return currentPackage.getUml2Package();				
			}
		}
		return null;
	}
	
        
    /**
     * Tells if the model can use GLib.
     * 
     * @return 
     */
    public boolean useGlib(){
        return !hasStereotype("noGlib");
    }
    
    /**
     * Tells if the model can use C99 specific types.
     * 
     * @return 
     */
    public boolean useC99(){
        return hasStereotype("c99");
    }   
 
	/**
	 * Check if the library is imported in the model.
	 * 
	 * @param libName
	 * 
	 * @return 
	 */
	public boolean hasLibrary(String libName) {
		boolean found = false;
		
		Iterator it = getUml2Model().getImportedPackages().iterator();
		while (it.hasNext() && !found) {
			org.eclipse.uml2.uml.Package currentLibrary = (org.eclipse.uml2.uml.Package) it.next();
			
			if (libName.equals(currentLibrary.getName())) {
				found = true;
			}
		}
		
		return found;
	}
	
	/**
	 * Check if the profile is applied to the model.
	 * 
	 * @param profileName 
	 * 
	 * @return 
	 */
	public boolean hasProfile(String profileName) {
		boolean found = false;
		
		Iterator it = getUml2Model().getAllAppliedProfiles().iterator();
		while (it.hasNext() && !found) {
			org.eclipse.uml2.uml.Profile currentProf = (org.eclipse.uml2.uml.Profile) it.next();
			
			if (profileName.equals(currentProf.getName())) {
				found = true;
			}
		}
		
		return found;
	}
}